(function() {


    angular.module('BlurAdmin.pages.UserlistM')
        .controller('DeviceListCtrl', DeviceList);

    function DeviceList($scope, tongxin, $http, $myconfig) {
        console.log(123123123);
        var oo = tongxin.getUserInfo();
        var modality = $scope.modality = oo.modality;
        console.log(modality);
        $scope.modality[0].durations = Math.floor(parseInt(modality[0].durations) / 60);
        $scope.modality[1].durations = Math.floor(parseInt(modality[1].durations) / 60);


        var yzb_data = {
            name: '使用时长',
            datapoints: [
                { x: 2001, y: 12 },
                { x: 2002, y: 155 },
                { x: 2003, y: 102 },
                { x: 2003, y: 102 }
            ]
        };
        var jbg_data = {
            name: '使用时长',
            datapoints: [
                { x: 2001, y: 12 },
                { x: 2002, y: 155 },
                { x: 2003, y: 102 },
                { x: 2003, y: 102 }
            ]
        };



        function getpar(name, id) {
            $http.get($myconfig.api.users_users_count_mode + '?dev_name=' + name + '&user_id=' + id).then(function(res) {
                console.log(res);
                var i = 0;
                var arr = [
                    { x: 2001, y: 12 },
                    { x: 2002, y: 155 },
                    { x: 2003, y: 102 },
                    { x: 2003, y: 102 }
                ];
                legend_arr = [];
                for (i; i < res.data.data.length; i++) {
                    arr[i].y = ~~(res.data.data[i].durations / 60);
                    arr[i].x = res.data.data[i].mode;
                    legend_arr.push(res.data.data[i].mode);
                }
                if (name == '金鼻光') {
                    $scope.jbg_config = legend_arr;
                    var o = JSON.stringify($scope.jbg_config);
                    var p = JSON.parse(o);
                    jbg_data.datapoints = arr.slice(0, i);
                    $scope.modality[1].name == '金鼻光' && ($scope.modality[1].pageload = jbg_data) && ($scope.modality[1].config = p);
                    $scope.modality[0].name == '金鼻光' && ($scope.modality[0].pageload = jbg_data) && ($scope.modality[0].config = p);
                }
                if (name == '焕颜宝') {
                    yzb_data.datapoints = arr.slice(0, i);
                    $scope.jbg_config = legend_arr;
                    var o = JSON.stringify($scope.jbg_config);
                    var p = JSON.parse(o);
                    $scope.modality[1].name == '焕颜宝' && ($scope.modality[1].pageload = yzb_data) && ($scope.modality[1].config = p);
                    $scope.modality[0].name == '焕颜宝' && ($scope.modality[0].pageload = yzb_data) && ($scope.modality[0].config = p);

                    // $scope.modality[1].pageload = yzb_data;
                }


            });
        }

        getpar("金鼻光", oo.id);
        getpar("焕颜宝", oo.id);


        $scope.informationData = [{
                num: 1,
                browser: "张三的金鼻光",
                text: "12分钟",
                qiangdu: '100',
                name: '金鼻光',
                addaers: '102993940823908432',
                start_data: '2011/02/02',
                end_data: '2016/06/18',
                user: '张三',
                pageload: {
                    name: '种类',
                    datapoints: [
                        { x: '调血气', y: 46 },
                        { x: '鼻炎', y: 999 },
                        { x: '净血', y: 12 },
                        { x: '通筋络', y: 12 }
                    ]
                }
            },

            {
                num: 1,
                browser: "张三的金鼻光",
                text: "96分钟",
                qiangdu: '100',
                name: '换颜宝',
                addaers: '102993940823908432',
                start_data: '2011/02/02',
                end_data: '2016/06/18',
                user: '张三',
                pageload: {
                    name: '种类',
                    datapoints: [
                        { x: '调血气', y: 100 },
                        { x: '鼻炎', y: 100 },
                        { x: '净血', y: 100 },
                        { x: '通筋络', y: 100 }
                    ]
                }
            }



        ];

        //饼状图数据
        $scope.myname = '';
        $scope.tpo = 1;
        $scope.myTabs = function(val) {
            $scope.tpo = val;
        }
        var pageload = {
            name: '种类',
            datapoints: [
                { x: '调血气', y: 200 },
                { x: '鼻炎', y: 100 },
                { x: '净血', y: 45 },
                { x: '通筋络', y: 130 }
            ]
        };

        var legend_arr = [];
        $scope.jbg_config = {
            title: {
                text: $scope.myname,
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{b} : {c} 分钟"
            },
            legend: { //点击类别显示
                show: true,
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} 分钟"
                },
                orient: 'vertical',
                y: 'center',
                x: '220px',
                padding: '20',
                data: legend_arr
            },
            series: [{
                itemStyle: {
                    normal: {
                        label: {
                            position: 'inner',
                            formatter: function(a, b, c, d) {
                                return (d - 0).toFixed(0) + '%';
                            }
                        },
                        labelLine: { show: false }
                    },
                    emphasis: {
                        label: {
                            show: true,
                            formatter: '22%'
                        }
                    }
                },

                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['60%', '55%'],
                data: [
                    // { name: "调血气", value: 100 },
                    // { name: "鼻炎", value: 100 },
                    // { name: "净血", value: 100 },
                    // { name: "通筋络", value: 100 }
                ]
            }]
        };
        $scope.data = [pageload];

    }

})();